<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流水线详情</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
    <style>
      .layui-quote-red{border-style:solid;border-color:#FF5722;border-width:1px 1px 1px 5px;background:0 0}
      .layui-quote-warn{border-style:solid;border-color:#FFB800;border-width:1px 1px 1px 5px;background:0 0}
      .grid-demo-line{background-color: #31BDEC;height: 2px;width:100%;margin-top:40px;z-index:-999;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
            <blockquote class="layui-elem-quote layui-quote-nm">
              <span class="layui-text" id="createTime"></span>
              <span class="layui-text">&nbsp;&nbsp;|&nbsp;&nbsp;</span>
              <span class="layui-text" id="pipelineId"></span>
              <span class="layui-text">&nbsp;&nbsp;|&nbsp;&nbsp;</span>
              <span class="layui-text" id="triggerMode"></span>
              <span class="layui-text">&nbsp;&nbsp;|&nbsp;&nbsp;</span>
              <span class="layui-text" id="pipelineRunId"></span>
              <span class="layui-text">&nbsp;&nbsp;|&nbsp;&nbsp;状态:</span>
              <span id="status"></span>
              <div>
                <span id="gitRepo"></span>             
              </div>
              <div style="margin-top:15px;">
                <button class="layui-btn" id="RunJob">运行</button>
                <button class="layui-btn layui-btn-danger" id="Stop">停止</button>
                <button class="layui-btn layui-btn-normal" id="Edit">编辑</button>
              </div>
            </blockquote>

            <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
              <ul class="layui-tab-title">
                <li class="layui-this">最近运行</li>
                <li>运行历史</li>
              </ul>
              <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                  <div class="flowStageTpl"></div>
                </div>
                <div class="layui-tab-item">
                  <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>  
                </div>
              </div>
            </div> 
            
    </div>
</div>
</body>
<script type="text/html" id="runIdTpl">
     <span style="color:#1E9FFF">#{{ d.pipelineRunId }}</span>
</script>	
<script type="text/html" id="statusTpl">
  {{# if (d.status == 'SUCCESS') { }}
    <span class="layui-font-green layui-font-16">成功</span>
  {{# } else if (d.status == 'RUNNING') { }}
    <span class="layui-font-orange layui-font-16">运行中</span>
	{{# } else { }}
    <span class="layui-font-red layui-font-16">失败</span>
{{# } }}
</script>	
<script type="text/html" id="sTimeTpl">
    {{# var tt = getTime(d.startTime);}}
    <span>{{ tt }}</span>
</script>	
<script type="text/html" id="eTimeTpl">
    {{# var tt = getTime(d.endTime);}}
    <span>{{ tt }}</span>
</script>	
<script type="text/html" id="modeTpl">
  {{# if (d.triggerMode == 1) { }}
     <span style="color:#218868">人工触发</span>
  {{# } else if (d.triggerMode == 2) { }}
     <span style="color:#218868">定时触发</span>
  {{# } else if (d.triggerMode == 3) { }}
     <span style="color:#218868">代码提交触发</span>
  {{# } else if (d.triggerMode == 4) { }}
     <span style="color:#218868">流水线触发</span>
	{{# } else { }}
    <span style="color:#7A8B8B">WEBHOOK触发</span>
{{# } }}
</script>	
<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="detail">详情</a>
</script>

<script>

  var allStatus = ''; //定义全局变量流水线的状态
  var lastRunTime = '';
  var pipelineRunId = getQueryString("pipelineRunId");

  //转换时间戳为时间
  function getTime(timestamp) {
      var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear(),
          M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1),
          D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()),
          h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()),
          m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()),
          s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
      return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  }

  //转换状态显示
  function getStatus(status) {
    if (status == "SUCCESS"){
      return '<i class="layui-text layui-font-green layui-font-16">成功</i>';     
    }else if (status == "RUNNING"){
      return '<i class="layui-text layui-font-orange layui-font-16">运行中</i>';
    }else if (status == "WAITING"){
      return '<i class="layui-text layui-font-orange layui-font-16">等待中</i>';
    }else if (status == "FAIL"){
      return '<i class="layui-text layui-font-red layui-font-16">失败</i>';
    }else{
      return '<i class="layui-text layui-font-blue layui-font-16">'+status+'</i>';
    }
  }

  //转换触发模式
  function getTriggerMode(obj) {
    if (obj == 1){
      return "人工触发";      
    }else if (obj == 2){
      return "定时触发";
    }else if (obj == 3){
      return "代码提交触发";
    }else if (obj == 5){
      return "流水线触发";
    }else if (obj == 6){
      return "WEBHOOK触发";
    }else{
      return obj;
    }    
  }

  //重试任务
  function RetryJob(objId) {
      layer.confirm('确定重试?', {icon: 3, title:'提示',yes: function(index){
           var index2 = layer.load(0, {shade: false});
           layer.msg('此处需运行3-5s左右');
           $.ajax({
             url: "/cicd/pipeline/Retry" + location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
             type: "POST",
             data: JSON.stringify(data.field),
             dataType: "json",
             success: function (resp) {
                      layer.close(index2);
                       if(resp.code == 0){
                          layer.msg('重试成功', {icon: 1});
				                  //window.location.reload();
                       }else{
                          layer.msg(resp.msg,{icon:2});
                       }
              }
            });		  	  
        },
        cancel: function(index, layero){ 
          layer.close(index);
          layer.close(index2);
          console.log("不操作");
        } 
      }); 
  }

  //通过确认
  function PassJob(objId) {
       $.ajax({
         url: "/cicd/pipeline/PassValidate" + location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
         type: "POST",
         data: JSON.stringify(data.field),
         dataType: "json",
         success: function (resp) {
                  layer.close(index2);
                   if(resp.code == 0){
                      layer.msg('通过成功', {icon: 1});
		                  //window.location.reload();
                   }else{
                      layer.msg(resp.msg,{icon:2});
                   }
          }
        });		  	  
  }

  //拒绝确认
  function PassJob(objId) {
       $.ajax({
         url: "/cicd/pipeline/RefuseValidate" + location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
         type: "POST",
         data: JSON.stringify(data.field),
         dataType: "json",
         success: function (resp) {
                  layer.close(index2);
                   if(resp.code == 0){
                      layer.msg('拒绝成功', {icon: 1});
		                  //window.location.reload();
                   }else{
                      layer.msg(resp.msg,{icon:2});
                   }
          }
        });		  	  
  }

  //停止job
  function StopJob(objId) {
       $.ajax({
         url: "/cicd/pipeline/StopJob" + location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
         type: "POST",
         data: JSON.stringify(data.field),
         dataType: "json",
         success: function (resp) {
                  layer.close(index2);
                   if(resp.code == 0){
                      layer.msg('停止成功', {icon: 1});
		                  //window.location.reload();
                   }else{
                      layer.msg(resp.msg,{icon:2});
                   }
          }
        });		  	  
  }

  //跳过job
  function SkipJob(objId) {
       $.ajax({
         url: "/cicd/pipeline/SkipJob" + location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
         type: "POST",
         data: JSON.stringify(data.field),
         dataType: "json",
         success: function (resp) {
                  layer.close(index2);
                   if(resp.code == 0){
                      layer.msg('跳过成功', {icon: 1});
		                  //window.location.reload();
                   }else{
                      layer.msg(resp.msg,{icon:2});
                   }
          }
        });		  	  
  }

  function ViewLog(objId) {
    //console.log(obj);   
    layer.open({
      title: '日志:'+objId,
      type: 2,
      shade: 0.2,
      maxmin:true,
      shadeClose: true,
      area: ['70%', '90%'],
      content: '/page/cicd/pipelines_log.html'+ location.search+"&pipelineRunId="+pipelineRunId+"&jobId="+objId,
    }); 
  }

  //状态提交
  function PostStatus(status,lastRunTime) {
    var state = 1;
    if (status == "SUCCESS"){
      state = 1;
    }else if (status == "RUNNING"){
      state = 2;
    }else if (status == "FAIL"){
      state = 3;
    }else{
      state = 1;
    }
       $.ajax({
         url: "/cicd/v1/PostStatus" + location.search+"&status="+state+"&lastRunTime="+lastRunTime,
         type: "GET",
         dataType: "json",
         success: function (resp) {
                   if(resp.code != 0){
                      //layer.msg('拒绝成功', {icon: 1});                    
		                  //window.location.reload();
                      console.log(resp);
                   }
          }
        });		  	  
  }

  function getPipelineRun(objId){
			$.get('/cicd/pipeline/GetRun' + location.search+"&pipelineRunId="+objId, function (resp) {
  				if (resp.success == true ) {                       
              var triggerMode = resp.pipelineRun.triggerMode;
              var pipelineId = resp.pipelineRun.pipelineId;
              var status = resp.pipelineRun.status;
              allStatus = resp.pipelineRun.status;
              var createTime = resp.pipelineRun.createTime;
              var pipelineRunId = resp.pipelineRun.pipelineRunId;
              lastRunTime = getTime(createTime);
            
              $('#triggerMode').html('触发模式:'+getTriggerMode(triggerMode));
              $('#pipelineId').html('流水线ID:'+pipelineId);
              $('#createTime').html('创建时间:'+getTime(createTime));
              $('#pipelineRunId').html('运行ID:'+pipelineRunId);
              $('#status').html(getStatus(status));
                                      
              var total = resp.pipelineRun.sources.length;
              //var stagesLength = resp.pipelineRun.stageGroup.length;
              var stagesLength = resp.pipelineRun.stages.length;
              var stages = resp.pipelineRun.stages;
              var jobs0Length = resp.pipelineRun.stages[0].stageInfo.jobs.length;
              var gitRepo = resp.pipelineRun.sources[0].data.repo;
              var gitBranch = resp.pipelineRun.sources[0].data.branch;
              $('#gitRepo').html('Git: '+gitRepo+'  分支: '+gitBranch);
              
              //var currStatus = '';
              //var lastStatus = '';
              
              if ( total == 1 && stagesLength >= 2 && jobs0Length == 1) {
                console.log("aa");
                  var headTpl = '<div class="layui-row">'
                  var itemTpl = '';
                  //console.log(stagesLength);
                  $.each(stages,function(i,item){
                    //情况为：一个流水线只有一个任务："Group0-Stage0","Group1-Stage0"的情况                    
                      //currStatus += stages[i].stageInfo.status + ",";
                      $.each(stages[i].stageInfo.jobs,function(j,item2){                            
                        var statusTpl = '';
                        var colorBlockTpl = '';
                        var logbtnTpl = '<input class="layui-btn layui-btn-xs" type="button" id="logbtn" onclick="ViewLog('+stages[i].stageInfo.jobs[j].id+');" value="日志" />';
                        var jobLength = stages[i].stageInfo.jobs.length;
                        //状态显示
                        if (stages[i].stageInfo.jobs[j].status == "SUCCESS"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-primary layui-border-green">成功</div>'
                        }else if (stages[i].stageInfo.jobs[j].status == "FAIL"){
                          //statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-danger">失败</div>'
                          statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="Retrybtn" onclick="RetryJob('+stages[i].stageInfo.jobs[j].id+');" value="重试" />'
                          colorBlockTpl = 'layui-quote-red';
                        }else if (stages[i].stageInfo.jobs[j].status == "RUNNING"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-warm">运行中</div>'
                          colorBlockTpl = 'layui-quote-warn';
                        }else if (stages[i].stageInfo.jobs[j].status == "WAITING"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-warm">等待中</div>'
                          colorBlockTpl = 'layui-quote-warn';
                        }else {
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-disabled">'+stages[i].stageInfo.jobs[j].status+'</div>'
                          colorBlockTpl = 'layui-quote-nm';
                          logbtnTpl = '';
                        }
                        //耗时计算
                        var runTimeTpl = '';
                        var runTime = (stages[i].stageInfo.jobs[j].endTime - stages[i].stageInfo.jobs[j].startTime) / 1000;
                        if (isNaN(runTime)){
                          runTime = 0;
                        }else{
                          runTimeTpl = '<div class="layui-btn layui-btn-xs layui-btn-primary layui-btn-disabled">'+runTime+'s</div>';
                        }
                        //人工卡点判断
                        var sjob = stages[i].stageInfo.jobs[j]
                        if (sjob.hasOwnProperty('actions')) {
                            if (stages[i].stageInfo.jobs[j].actions.length == 0 ){
                              logbtnTpl = '';
                            }else{
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "PassPipelineValidate" || stages[i].stageInfo.jobs[j].actions[0].type =="RefusePipelineValidate" ){
                                statusTpl = '<input class="layui-btn layui-btn-xs" type="button" id="logbtn" onclick="PassJob('+stages[i].stageInfo.jobs[j].id+');" value="通过" />';
                                runTimeTpl = '<input class="layui-btn layui-btn-xs layui-btn layui-btn-danger" type="button" id="logbtn" onclick="RefuseJob('+stages[i].stageInfo.jobs[j].id+');" value="拒绝" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "RetryPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="RetryJob('+stages[i].stageInfo.jobs[j].id+');" value="重试" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "StopPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="StopJob('+stages[i].stageInfo.jobs[j].id+');" value="取消" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "SkipPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="SkipJob('+stages[i].stageInfo.jobs[j].id+');" value="跳过" />';
                              }
                            }
                        }else{
                          logbtnTpl = '';
                        }
                        //console.log(statusTpl);
                        if ( i == stagesLength -1 && j == jobLength -1 ) { //不到最后一个都得加上向导符号：==>
                          itemTpl += '<div class="layui-col-xs2">' +
                                '<blockquote class="layui-elem-quote layui-input-inline '+colorBlockTpl+'" style="width:160px;">' +
                                  '<div class="layui-text" style="margin-top:-5px;">'+stages[i].stageInfo.jobs[j].name+'</div><br>' +
                                  '<div class="layui-inline" style="margin-top:-20px;">' +
                                    statusTpl+
                                    runTimeTpl +
                                    logbtnTpl +
                                  '</div>' +
                                '</blockquote>'+
                              '</div>'
                        }else{
                          itemTpl += '<div class="layui-col-xs2">' +
                              '<div class="layui-inline">' +
                                '<blockquote class="layui-elem-quote layui-input-inline '+colorBlockTpl+'" style="width:160px;">' +
                                  '<div class="layui-text" style="margin-top:-5px;">'+stages[i].stageInfo.jobs[j].name+'</div><br>' +
                                  '<div class="layui-inline" style="margin-top:-20px;">' +
                                    statusTpl+
                                    runTimeTpl +
                                    logbtnTpl +
                                  '</div>' +
                                '</blockquote>' +
                                 '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' +  
                                '<div class="layui-input-inline layui-btn layui-btn-xs layui-btn-disabled"><i class="layui-icon">&#xe65b;</i></div>' +
                              '</div>' +
                          '</div>'       
                        }
                      });
                  });
                var endTpl ='</div>'
                allTpl = headTpl + itemTpl +endTpl;
                
                //if (currStatus == lastStatus){
                //  console.log(currStatus,lastStatus);
                //  return
                //}else{
                //  lastStatus = currStatus
                //}
                
                $('.flowStageTpl').empty();
                $('.flowStageTpl').append(allTpl);
              }else{ //情况为一个流水线多个任务的情况："Group0-Stage0","Group0-Stage1","Group0-Stage2","Group0-Stage3"
                console.log("bb");
                  $.each(stages,function(i,item){
                        var headTpl = '<div class="layui-row">';
                        var itemTpl = '';
                        var jobLength = stages[i].stageInfo.jobs.length;
                        var stageInfoStatus = stages[i].stageInfo.status;
                      $.each(stages[i].stageInfo.jobs,function(j,item2){
                        var statusTpl = '';
                        var colorBlockTpl = '';
                        var logbtnTpl = '<input class="layui-btn layui-btn-xs" type="button" id="logbtn" onclick="ViewLog('+stages[i].stageInfo.jobs[j].id+');" value="日志" />';
                        //状态显示
                        if (stages[i].stageInfo.jobs[j].status == "SUCCESS"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-primary layui-border-green">成功</div>'
                        }else if (stages[i].stageInfo.jobs[j].status == "FAIL"){
                          //statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-danger">重试</div>'
                          statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="Retrybtn" onclick="RetryJob('+stages[i].stageInfo.jobs[j].id+');" value="重试" />'
                          colorBlockTpl = 'layui-quote-red';
                        }else if (stages[i].stageInfo.jobs[j].status == "RUNNING"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-warm">运行中</div>'
                          colorBlockTpl = 'layui-quote-warn';
                        }else if (stages[i].stageInfo.jobs[j].status == "WAITING"){
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-warm">等待中</div>'
                          colorBlockTpl = 'layui-quote-warn';
                        }else {
                          statusTpl = '<div class="layui-btn layui-btn-xs layui-btn-disabled">'+stages[i].stageInfo.jobs[j].status+'</div>'
                          colorBlockTpl = 'layui-quote-nm';
                          logbtnTpl = '';
                        }
                        //耗时计算
                        var runTimeTpl = '';
                        var runTime = (stages[i].stageInfo.jobs[j].endTime - stages[i].stageInfo.jobs[j].startTime) / 1000;
                        if (isNaN(runTime)){
                          runTime = 0;
                        }else{
                          runTimeTpl = '<div class="layui-btn layui-btn-xs layui-btn-primary layui-btn-disabled">'+runTime+'s</div>';
                        }
                        //人工卡点判断
                        var sjob = stages[i].stageInfo.jobs[j]
                        if (sjob.hasOwnProperty('actions')) {
                            if (stages[i].stageInfo.jobs[j].actions.length == 0 ){
                              logbtnTpl = '';
                            }else{
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "PassPipelineValidate" || stages[i].stageInfo.jobs[j].actions[0].type =="RefusePipelineValidate" ){
                                statusTpl = '<input class="layui-btn layui-btn-xs" type="button" id="logbtn" onclick="PassJob('+stages[i].stageInfo.jobs[j].id+');" value="通过" />';
                                runTimeTpl = '<input class="layui-btn layui-btn-xs layui-btn layui-btn-danger" type="button" id="logbtn" onclick="RefuseJob('+stages[i].stageInfo.jobs[j].id+');" value="拒绝" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "RetryPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="RetryJob('+stages[i].stageInfo.jobs[j].id+');" value="重试" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "StopPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="StopJob('+stages[i].stageInfo.jobs[j].id+');" value="取消" />';
                              }
    
                              if (stages[i].stageInfo.jobs[j].actions[0].type == "SkipPipelineJobRun"){
                                statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-danger" type="button" id="logbtn" onclick="SkipJob('+stages[i].stageInfo.jobs[j].id+');" value="跳过" />';
                              }
                            }
                        }else{
                          logbtnTpl = '';
                        }
                        
                        //手动触发判断
                        //console.log(stageInfoStatus); 
                        if(stageInfoStatus == "SWITCH_MANUAL" && j == 0) { //当有配置手动触发时在jobs数组的第一个更改status为按钮
                          statusTpl = '<input class="layui-btn layui-btn-xs layui-btn-primary layui-border-black" type="button" id="logbtn" onclick="ManualRun('+stages[i].stageInfo.jobs[j].id+');" value="等待手动触发" />';
                        }                            
                        
                        if ( j < jobLength - 1 ) { //不到最后一个都得加上向导符号：==>                   
                          itemTpl += '<div class="layui-col-xs2">' +
                              '<div class="layui-inline">' +
                                '<blockquote class="layui-elem-quote layui-input-inline '+colorBlockTpl+'" style="width:160px;">' +
                                  '<div class="layui-text" style="margin-top:-5px;">'+stages[i].stageInfo.jobs[j].name+'</div><br>' +
                                  '<div class="layui-inline" style="margin-top:-20px;">' +
                                    statusTpl+
                                    runTimeTpl +
                                    logbtnTpl +
                                  '</div>' +
                                '</blockquote>' +
                                 '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' +  
                                '<div class="layui-input-inline layui-btn layui-btn-xs layui-btn-disabled"><i class="layui-icon">&#xe65b;</i></div>' +
                              '</div>' +
                          '</div>'
                        }else{
                          itemTpl += '<div class="layui-col-xs2">' +
                                '<blockquote class="layui-elem-quote layui-input-inline '+colorBlockTpl+'" style="width:160px;">' +
                                  '<div class="layui-text" style="margin-top:-5px;">'+stages[i].stageInfo.jobs[j].name+'</div><br>' +
                                  '<div class="layui-inline" style="margin-top:-20px;">' +
                                    statusTpl+
                                    runTimeTpl +
                                    logbtnTpl +
                                  '</div>' +
                                '</blockquote>'+
                              '</div>'          
                        }
                      }); 
                      var endTpl ='</div>'
                      allTpl = headTpl + itemTpl +endTpl;
                      //console.log(allTpl);

                      $('.flowStageTpl').empty();
                      $('.flowStageTpl').append(allTpl);
                  });                     
              }

          }else{
            layer.msg('请重新刷新',{icon:2});
          }
		  });
  }

//end getPipelineRun func

    layui.use(['form', 'table','miniTab','element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            element = layui.element,
            table = layui.table;

        //编辑
        $('#Edit').on("click",function(){
                layer.open({
                    title:'流水线编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '90%'],
                    content: '/page/cicd/pipelines_edit.html'+ location.search,
                });
        });

        //运行
        $('#RunJob').on("click",function(){
            var index2 = layer.load(0, {shade: false});
            $.ajax({
             url: "/cicd/pipeline/Start" + location.search,
             type: "GET",
             dataType: "json",
             success: function (resp) {
                        layer.close(index2);
                        if(resp.success == true){
                          pipelineRunId = resp.pipelineRunId;
                          PostStatus('RUNNING');
                          layer.msg('启动成功', {icon: 1,time:2000},function(){
                              location.reload();
                          });                          
                          //getPipelineRun(pipelineRunId);
				                  //window.location.reload();
                        }else{     
                          layer.msg(resp.errorMessage,{icon:2});
                        }
              },
              fail:function(){
                layer.msg("调用启动失败,请重试!",{icon:2},function(){
                  layer.close(index2);
                });                
              }
            });		  	  
        });

        //停止流水线
        $('#Stop').on("click",function(){
            layer.confirm('确定停止?', {icon: 3, title:'提示',yes: function(index){
                 var index2 = layer.load(0, {shade: false});
                 layer.msg('此处需运行3-5s左右');
                 $.ajax({
                    url: "/cicd/pipeline/GetRun" + location.search,
                    type: "GET",
                    dataType: "json",
                    success: function (resp) {
                        if (resp.pipelineRun.status == "RUNNING ") {
                            $.ajax({
                             url: "/cicd/pipeline/Stop" + location.search,
                             type: "POST",
                             data: JSON.stringify(data.field),
                             dataType: "json",
                             success: function (resp) {
                                      layer.close(index2);
                                       if(resp.success == true){
                                          layer.msg('停止成功', {icon: 1});
                                       }else{
                                          layer.msg(resp.errorMessage,{icon:2});
                                       }
                              }
                            });		
                        }else{
                          layer.msg('任务已经停止', {icon: 1});
                        }  
                    }
                  });			  
              },
              cancel: function(index, layero){ 
                layer.close(index);
                layer.close(index2);
                console.log("不操作");
              } 
            }); 
        });

        table.render({
            elem: '#currentTableId',
            url: '/cicd/pipeline/ListRun' + location.search+"&num=10",
            cols: [[
                {field: 'pipelineRunId',width: 150, title: '部署记录',templet: '#runIdTpl', sort: true},
                {field: 'status',title: '状态',templet: '#statusTpl',},
                {field: 'startTime', title: '开始时间',templet: '#sTimeTpl',},
                {field: 'endTime',title: '结束时间',templet: '#eTimeTpl',},
                {field: 'pipelineId', title: '流水线ID'},
                {field: 'triggerMode', title: '触发模式',templet: '#modeTpl',},
                {title: '操作', minWidth: 250, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 25,
            page: true,
            response: {
              statusCode: true //重新规定成功的状态码为true，table 组件默认为 0
            },
            parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
              return {
                "code": res.success, //解析接口状态
                "msg": res.errorMessage, //解析提示文本
                "count": res.totalCount, //解析数据长度
                "data": res.pipelineRuns //解析数据列表
              };
            }           
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                var index = layer.open({
                    title: '详情',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['97%', '97%'],
                    content: '/page/cicd/pipelines_detail.html'+location.search+'&pipelineRunId='+data.pipelineRunId,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'setDefaultCluster') {               
                console.log(data);               
            }
        });
        
        var index2 = layer.load(0, {shade: false});
        $.ajax({
           url: "/cicd/pipeline/ListRun"+location.search+"&num=1",
           type: "GET",
           success: function (resp) {
                if (resp.success == false) {
                   layer.close(index2);	
                   console.log('获取pipelineRunId Fail:'+resp.msg);
                   layer.msg('暂没有运行记录,可点击运行',{icon:1});
                   return
                }
                if (pipelineRunId == null || pipelineRunId == ""){
                  pipelineRunId = resp.pipelineRuns[0].pipelineRunId;
                }
                getPipelineRun(pipelineRunId);
                layer.close(index2);	
            },
            fail:function(){
              layer.close(index2);	
            }
         });



  	const timer2 = setInterval(function () {
      //$("#container").empty();
      //console.log(allStatus);
      if (allStatus != "RUNNING" && allStatus != "WAITING") {
          PostStatus(allStatus,lastRunTime);
          clearInterval(timer2);
          return
      }
      getPipelineRun(pipelineRunId);
  	},3000);	

    setTimeout(() => { //1小时后停止time2
      clearInterval(timer2);
      console.log("Timer2 stop");
    }, 1800000);

    });
</script>
</html>